package com.baomidou.springwind.common.business;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.springwind.controller.NodeData;
import com.baomidou.springwind.pojo.SiteSelection;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class network {
    /**
     * 根据map生成echarts地图需要的json地图格式
     * @param map
     */
    public static JSONArray buildMap(Map<SiteSelection, List<SiteSelection>> map){
        JSONArray data = new JSONArray();
        String startnode=null;
        Random random = new Random(47);
        for (Map.Entry<SiteSelection,List<SiteSelection>> entry:map.entrySet()){
            JSONArray nodes = new JSONArray();
            JSONArray res = new JSONArray();
            JSONObject serie=new JSONObject();
            startnode=entry.getKey().getName();
            for(int j=0;j<entry.getValue().size();j++){
                nodes.add(new NodeData(startnode, Arrays.asList(entry.getValue().get(j).getCoordinateX(),entry.getValue().get(j).getCoordinateY(), random.nextInt(100))));
                JSONObject res1 = parseRes(entry,j);
                res.add(res1);
            }
            serie.put("name",startnode);
            serie.put("res", res);
            serie.put("nodes", nodes);
            data.add(serie);
        }
        return data;
    }

    /**
     * 对res进行处理
     * @param entry
     * @param j
     * @return
     */
    private static JSONObject parseRes(Map.Entry<SiteSelection,List<SiteSelection>> entry,int j) {
        JSONObject res = new JSONObject();
        res.put("fromName",entry.getKey().getName());
        res.put("toName", entry.getValue().get(j).getName());
        JSONArray fromCoord = new JSONArray();
        fromCoord.add(entry.getKey().getCoordinateX());
        fromCoord.add(entry.getKey().getCoordinateY());
        JSONArray toCoord = new JSONArray();
        toCoord.add(entry.getValue().get(j).getCoordinateX());
        toCoord.add(entry.getValue().get(j).getCoordinateY());
        JSONArray coords = new JSONArray();
        coords.add(fromCoord);
        coords.add(toCoord);
        res.put("coords", coords);
        return res;
    }
}
